﻿
using HBase;
using HBase.cache;
using hCommand.dto;
using hCommand.entity; 
using System;
using System.Collections.Generic; 

namespace hCommand
{
    public class WcsLoadUp : ZtTask {
        public static Dictionary<long,string > user2openDict = new Dictionary<long,string >();
        private ISysServer sysServer = WMSDalFactray.getDal<ISysServer>();
        ISysAuthorityServer authorityServer = WMSDalFactray.getDal<ISysAuthorityServer>();



        public static CyUser getEqupUser(CyEqup e) {
            CyUser u = null;
            if (user2openDict.ContainsKey(e.userId)) {
                String openId = user2openDict[e.userId];
                 u = MyCache.getValue<CyUser>("CyUser", openId);
            }

            return u;
        }

        public WcsLoadUp() {
            time = 20000;
        }

        void addUserCache(CyUser u) {
            MyCache.addOver(u);
            if (!user2openDict.ContainsKey(u.id)) {
                user2openDict.Add(u.id,u.openId );
            }
          
        }

        protected override bool onlyOneTimeAsync() {
            MyCache.setDirty<CyUser>(HUtil.getMyDate( DateTime.Now));
            List<CyUser> list = sysServer.GetAll<CyUser>(BaseConfig.BaseDBConnStr);

            for (int i = 0; i < list.Count; i++) {
                addUserCache(list[i]);
            }
            MyCache.setDirty<CyEqup>(HUtil.getMyDate(DateTime.Now));
            List<CyEqup> equpList = sysServer.GetAll<CyEqup>(BaseConfig.BaseDBConnStr);
            MyCache.addOver(equpList);
            for (int i = 0; i < equpList.Count; i++) {
                CyEqup e = equpList[i];


                CyUser u = getEqupUser( e);
                if (u != null) {
                    u.add(e);
                }
            }
            return true;
        }


     
        void updateUserCache( ) {
            long now = HUtil.getMyDate(DateTime.Now);
           
            List<CyUser> list = sysServer.getChange<CyUser>(MyCache.getLastDirty<CyUser>());
            MyCache.setDirty<CyUser>(now);
            if (list.Count > 0) {
                consoleOnly("CyUser" + list.Count);
            }
         
            if (list == null) {
                return;
            }
            foreach (CyUser u in list) {
                CyUser old = MyCache.getValue<CyUser>("CyUser", u.getId());
                if (old != null) {
                    u.setEqup(old.getEqup());
                }
                addUserCache(u);
            
            }
        }

        void updateEqupCache( ) {
            long now = HUtil.getMyDate(DateTime.Now);

            List<CyEqup> equpList = sysServer.getChange<CyEqup>(MyCache.getLastDirty<CyEqup>());
            MyCache.setDirty<CyEqup>(now);
            if (equpList.Count > 0) {
                consoleOnly("CyEqup" + equpList.Count);
            }
          


            if (equpList == null) {
                return;
            }
            foreach (CyEqup e in equpList) {
               var old= MyCache.getValue<CyEqup>("CyEqup", e.getId());
                if (old != null) {
                    CyUser oldUser = getEqupUser(old);
                    if (oldUser != null) {
                        List<CyEqup> l = oldUser.getEqup();
                        if (l != null) { 
                            l.Remove(old);
                        }
                    }
                }
                MyCache.addOver(e);
                CyUser u = getEqupUser(e);
          
                if (u != null) {
                    List<CyEqup> eList = u.getEqup();
                    if (eList != null) {
                        foreach (CyEqup e1 in eList) {
                            if (e1.id == e.id) {
                                eList.Remove(e1);
                                break;
                            }
                        }

                    }
                    u.add(e);
                }
            }
        }

   

        public override bool excute() {
        
            long now = DateTime.Now.Ticks;

            updateUserCache();
            updateEqupCache();
            List<BParam> pList = sysServer.getParams("cache", 1);
            foreach (BParam p in pList) {
                if (p.keyCode == "SysUser") {
                    checkSysUser(p.v);
                } else if (p.keyCode == "SysDict") {
                    check(p.v);
                }


            }
      
            return true;
        }


      

        void checkSysUser(long now)  {

            if (!MyCache.isDirty<SysUser>(now)) {
                return ;
            }
            List<SysUser> list = sysServer.getChange<SysUser>(MyCache.getLastDirty<SysUser>());
            MyCache.setDirty<SysUser>(now);
            consoleOnly("SysUser"  + list.Count);
            if (list == null) {
                return;
            }
            foreach (SysUser e in list) {
                MyCache.addOver(e); 
            }
       }
    


        void check(long ticks)  {
            if (MyCache.isDirty<SysDict>(ticks)) {
              
                long now = HUtil.getMyDate(DateTime.Now);
                List<DictRes> list = authorityServer.getAll<DictRes>(BaseConfig.BaseDBConnStr, "Sys_Dict", " and xg>=@time", new { time= MyCache.getLastDirty<SysDict>() });
                if (list == null) {
                    return;
                }
                foreach (DictRes e in list) {
                    MyCache.addOver(e);
                }
               
                MyCache.setDirty<SysDict>(now);
            }

        }
    }
}
